﻿
@{
    Layout = null;
}

<div>
    <div class="col-3 pr-0" style="display: none">
        <label id="SAMPLE-SelectSampleDefinition-Parent-ModalID">@ViewData["ModalID"]</label>
        <label id="SAMPLE-SelectSampleDefinition-SampleGroupId">@ViewData["SampleGroupId"]</label>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0 pl-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">实验室和组</span>
        </div>
        <div class="col-9 pl-0 input-group" style="padding-right: 0px;">
            <select id="SAMPLE-SelectSampleDefinition-Site" class="form-control font-rfs border-radius-noleft" style="width:50%"></select>
            <select id="SAMPLE-SelectSampleDefinition-Group" class="form-control font-rfs border-radius-noleft" style="width:50%"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0 pl-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">样品类型</span>
        </div>
        <div class="col-9 pl-0" style="padding-right: 0px;">
            <select id="SAMPLE-SelectSampleDefinition-Classification" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0 pl-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">样品分类</span>
        </div>
        <div class="col-9 pl-0" style="padding-right: 0px;">
            <select id="SAMPLE-SelectSampleDefinition-GenricClass" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0 pl-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">搜索</span>
        </div>
        <div class="col-9 pl-0" style="padding-right: 0px;">
            <input id="SAMPLE-SelectSampleDefinition-Name" class="form-control font-rfs border-radius-noleft" onchange="SearchName(this)" placeholder="按样品名称模糊搜索" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0 pl-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">批次号</span>
        </div>
        <div class="col-9 pl-0" style="padding-right: 0px;">
            <input id="SAMPLE-SelectSampleDefinition-Batch" class="form-control font-rfs border-radius-noleft border-radius-noright" />
        </div>
    </div>
</div>
<div class="table-responsive-sm modal-table-shadow p-0 bg-white rounded col-md-12 pre-scrollable" style="height: 60vh;">
    <table id="SAMPLE-SelectSampleDefinition-table" class="table table-striped table-bordered-modal table-sm table-hover">
        <thead>
            <tr>
                <th>ID</th>
                <th></th>
                <th>名称</th>
                <th>描述</th>
                <th>数量</th>
            </tr>
        </thead>
        <tbody style="font-size: calc(0.6em + 0.1vw) !important"></tbody>
    </table>
</div>
<div class="text-right" style="padding-top: 5px;">
    <button id="SAMPLE-SelectSampleDefinition-addSampleGroup" type="button" class="btn btn-primary font-rfs">添加</button>
    <button id="SAMPLE-SelectSampleDefinition-confirm" type="button" class="btn btn-primary font-rfs">确定</button>
    <button id="SAMPLE-SelectSampleDefinition-cancel" type="button" class="btn btn-info font-rfs">取消</button>
</div>

<script>
    var SampleDefinitionIdList = [];

    $(document).ready(function () {
        if ($("#SAMPLE-SelectSampleDefinition-SampleGroupId").html().length == 0) {
            $("#SAMPLE-SelectSampleDefinition-addSampleGroup").css("display", "none");
        } else {
            $("#SAMPLE-SelectSampleDefinition-confirm").css("display", "none");
        }
        LoadSampleDefinitionClassification("SAMPLE-SelectSampleDefinition-Classification");
        LoadSiteList("SAMPLE-SelectSampleDefinition-Site");

    });

    //根据选择的实验室加载工作组
    $("#SAMPLE-SelectSampleDefinition-Site").change(function () {
        LoadGroupList($("#SAMPLE-SelectSampleDefinition-Site").find("option:selected").text(), "SAMPLE-SelectSampleDefinition-Group");
    });

    $("#SAMPLE-SelectSampleDefinition-Classification").change(function () {
        $.post("GetSampleDefinitionGenricClass", { /* 加载 二级分类 信息*/
            Classification: $("#SAMPLE-SelectSampleDefinition-Classification").find("option:selected").text()  // 传递的参数，对应控制器里的参数
        }, function (data) {
            $("#SAMPLE-SelectSampleDefinition-GenricClass").empty();
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-SelectSampleDefinition-GenricClass").append(new Option(data[i].genricClass));
            }
            $("#SAMPLE-SelectSampleDefinition-GenricClass").prepend(new Option("", ""));
            $("#SAMPLE-SelectSampleDefinition-GenricClass").val("");

            LoadSampleDefinition();
        });
    });

    $("#SAMPLE-SelectSampleDefinition-GenricClass").change(function () {
        LoadSampleDefinition();
    });

    function SetEnableCortrol(value, row, index) {
        var strHtml = "";
        if (row.use) {
            strHtml = "<input type='checkbox' checked onclick='RefreshList(this)'> ";
        } else {
            strHtml = "<input type='checkbox' onclick='RefreshList(this)'> ";
        }

        return strHtml;
    }

    function RefreshList(obj){

        var id = $(obj).parent().parent().attr('data-uniqueid');
        var rowindex = $(obj).parent().parent().attr('data-index');
        var number = $("#SAMPLE-SelectSampleDefinition-table > tbody tr:eq(" + rowindex + ")").find("td:eq(3)").find("input:first").val();
        var item = id + '.' + number;

        var reg = /^[1-9]\d*$/;
        if (!reg.test(number)) {
            alert("数量只能为正整数！");
            return;
        }

        if ($(obj).prop("checked")) {
            SampleDefinitionIdList.push(item);
            //alert(SampleDefinitionIdList.toString());
        } else {
            var index = 0;
            SampleDefinitionIdList.forEach(function(value){
                if (id == value.split('.')[0]) {
                    SampleDefinitionIdList.splice(index, 1);
                }
                index++;
            });
            //alert(SampleDefinitionIdList.toString());
        }
    }

    function SetNumber(value, row, index) {
        var strHtml = "<input class='table-cell-input' value='1'/>";
        return strHtml;
    }

    function LoadSampleDefinition() {
        $('#SAMPLE-SelectSampleDefinition-table').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
        var columns = [{ field: "id", visible: false }, { field: "use", width: "5%", align: "center", formatter: SetEnableCortrol },
        { field: "name" }, { field: "description" }, { field: "number", width: "5%", align: "center", formatter: SetNumber }];

        var site = $("#SAMPLE-SelectSampleDefinition-Site").find("option:selected").text();  // 实验室和工作组
        var group = $("#SAMPLE-SelectSampleDefinition-Group").find("option:selected").text();

        $('#SAMPLE-SelectSampleDefinition-table').bootstrapTable({
            method: 'post',
            url: "GetSampleDefinitionListClassAndGenric",
            dataType: "json",  // 服务器端返回值类型
            contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
            queryParams: function (params) {
                var data = {
                    limit: params.limit, // 每页显示多少条记录
                    offset: params.offset, // 偏移量

                    Site: site,
                    Group: group,

                    Classification: $("#SAMPLE-SelectSampleDefinition-Classification").find("option:selected").text(),  // 传递的参数，这的名称要和控制器里对应的参数名称一样
                    GenricClass: $("#SAMPLE-SelectSampleDefinition-GenricClass").find("option:selected").text()
                };
                return data;
            },
            queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
            dataField: "rows",

            uniqueId: "id",          // 设置主键
            sidePagination: "server", // 服务器端分页，这个参数必需设置
            pagination: true,  // 显示分页条,设为true，启用了分页功能
            pageNumber: 1,
            pageSize: 10,   // 默认页大小
            pageList: [5, 10, 15, 20],  // 控制每页数目
            locale: "zh-CN",
            columns: columns,          // 加载列定义, 注意！这个地方绑定的列名称开头大写字母会全部变成小写。
            onClickRow: function (row, $element, field) {
                $("#SAMPLE-SelectSampleDefinition-table").find("tbody").find("tr").each(function () {
                    $(this).removeClass('changeblueColor');
                });
                $element.addClass("changeblueColor");
            },
            silent: true,  //刷新事件必须设置
            formatLoadingMessage: function () {
                return "请稍等，正在加载中...";
            },
            formatNoMatches: function () {  //没有匹配的结果
                return '无符合条件的记录';
            },
            onLoadError: function (data) {
                $('#SAMPLE-SelectSampleDefinition-table').bootstrapTable('removeAll');
            },
            onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                SetBootstrapStyle();
            },
            formatShowingRows: function (pageFrom, pageTo, totalRows) {
                return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
            },
            formatRecordsPerPage: function (pageNumber) {
                return " 每页显示" + pageNumber + '条记录';
            }
        });
    }

    function LoadSampleDefinitionByName(Name) {
        $('#SAMPLE-SelectSampleDefinition-table').bootstrapTable('destroy'); // 这行代码必须加，否则数据无法刷新
        var columns = [{ field: "id", visible: false }, { field: "use", width: "5%", align: "center", formatter: SetEnableCortrol },
        { field: "name" }, { field: "description" }, { field: "number", width: "5%", align: "center", formatter: SetNumber }];

        $('#SAMPLE-SelectSampleDefinition-table').bootstrapTable({
            method: 'post',
            url: "GetSampleDefinitionListByName",
            dataType: "json",  // 服务器端返回值类型
            contentType: "application/x-www-form-urlencoded; charset=UTF-8", // 如果是 POST 请求，这个属性必须加
            queryParams: function (params) {
                var data = {
                    limit: params.limit, // 每页显示多少条记录
                    offset: params.offset, // 偏移量
                    Name: Name  // 传递的参数，这的名称要和控制器里对应的参数名称一样
                };
                return data;
            },
            queryParamsType: "limit", // 服务器端分页，这个参数必需是“limit”
            dataField: "rows",

            uniqueId: "id",          // 设置主键
            sidePagination: "server", // 服务器端分页，这个参数必需设置
            pagination: true,  // 显示分页条,设为true，启用了分页功能
            pageNumber: 1,
            pageSize: 10,   // 默认页大小
            pageList: [5, 10, 15, 20],  // 控制每页数目
            locale: "zh-CN",
            columns: columns,          // 加载列定义, 注意！这个地方绑定的列名称开头大写字母会全部变成小写。
            onClickRow: function (row, $element, field) {
                $("#SAMPLE-SelectSampleDefinition-table").find("tbody").find("tr").each(function () {
                    $(this).removeClass('changeblueColor');
                });
                $element.addClass("changeblueColor");
            },
            silent: true,  //刷新事件必须设置
            formatLoadingMessage: function () {
                return "请稍等，正在加载中...";
            },
            formatNoMatches: function () {  //没有匹配的结果
                return '无符合条件的记录';
            },
            onLoadError: function (data) {
                $('#SAMPLE-SelectSampleDefinition-table').bootstrapTable('removeAll');
            },
            onLoadSuccess: function (data) {   // 数据加载成功后触发该事件
                SetBootstrapStyle();
            },
            formatShowingRows: function (pageFrom, pageTo, totalRows) {
                return "正在显示第" + pageFrom + "到第" + pageTo + "条记录，总共" + totalRows + "条记录 ";
            },
            formatRecordsPerPage: function (pageNumber) {
                return " 每页显示" + pageNumber + '条记录';
            }
        });
    }

    function SearchName(obj){
        LoadSampleDefinitionByName($(obj).val());
    }

    $("#SAMPLE-SelectSampleDefinition-cancel").click(function (event) {
        var modalID = $("#SAMPLE-SelectSampleDefinition-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });

    $("#SAMPLE-SelectSampleDefinition-confirm").click(function () {
        if (SampleDefinitionIdList.length > 0)
        {
            $.post("AddSampleLogin", {
                BatchId: $('#SAMPLE-SelectSampleDefinition-Batch').val(),
                SampleDefinitionList: SampleDefinitionIdList.toString()
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#SAMPLE-SelectSampleDefinition-cancel").click();
                } else {
                    alert(data);
                }
            });
        }
    });

    $("#SAMPLE-SelectSampleDefinition-addSampleGroup").click(function () {
        if (SampleDefinitionIdList.length > 0) {
            $.post("AddSampleToSampleGroup", {
                SampleDefinitionList: SampleDefinitionIdList.toString(),
                SampleGroupId: $("#SAMPLE-SelectSampleDefinition-SampleGroupId").html()
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#SAMPLE-SelectSampleDefinition-cancel").click();
                } else {
                    alert(data);
                }
            });
        }
    });

</script>
